package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.TravelComment;
import cn.wolfcode.luowowo.query.TravelCommentQuery;
import cn.wolfcode.luowowo.repository.TravelCommentRepository;
import cn.wolfcode.luowowo.service.ITravelCommentService;
import cn.wolfcode.luowowo.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService{

    @Autowired
    private TravelCommentRepository travelCommentRepository;

    @Autowired
    private MongoTemplate template;

    @Override
    public TravelComment get(String id) {
        return travelCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<TravelComment> listAll() {
    return travelCommentRepository.findAll();
    }

    @Override
    public void delete(String id) {
    travelCommentRepository.deleteById(id);
    }

    @Override
    public void save(TravelComment travelComment) {
        //内容过滤
        String content = travelComment.getContent();
        String regEx = "[日操傻逼]";
        content = Pattern.compile(regEx).matcher(content).replaceAll("*").trim();
        travelComment.setContent(content);
        //先将该游记评论的_id设置为空,
        travelComment.setId(null);
        //再设置发布时间为当前时间
        travelComment.setCreateTime(new Date());
        //引用评论对象
        String cid = travelComment.getRefComment().getId();
        //当cid有值时
        if (StringUtils.hasLength(cid)) {
            //则先获取到被回复的评论
            TravelComment refComment = this.get(cid);
            //再设置当前保存的评论的关联的评论
            travelComment.setRefComment(refComment);
            //再设置当前的评论的类型为评论的评论
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);
        }
        //最后执行保存操作
        travelCommentRepository.save(travelComment);
    }

    @Override
    public void update(TravelComment travelComment) {
    travelCommentRepository.save(travelComment);
    }

    @Override
    public Page<TravelComment> query(TravelCommentQuery qo) {
        //查询条件
        Query query = new Query();
        //高级查询条件
        if(StringUtils.hasLength(qo.getTravelId())){
            query.addCriteria(Criteria.where("travelId").is(qo.getTravelId()));
        }
        PageRequest pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
            Sort.Direction.DESC, "_id");
        Page<TravelComment> page = DBHelper.query(template, TravelComment.class, query, pageable);

        return page;
    }

    @Override
    public List<TravelComment> queryByTravelId(String travelId) {
        return travelCommentRepository.findByTravelId(travelId);
    }

    @Override
    public Page<TravelComment> getNewComments10() {
        Query query = new Query();
        Pageable pageable = PageRequest.of(0,10, Sort.Direction.DESC,"createTime");
        return DBHelper.query(template, TravelComment.class, query, pageable);
    }


}